#include "qwavechartview.h"
#include <QValueAxis>
#include <QDebug>
#include <thread>
#include <QScrollBar>

QWaveChartView::QWaveChartView()
{
    m_chart = new QChart();
    m_chart->legend()->hide();
    setChart(m_chart);

    m_line_series = new QList<QLineSeries*>();
//  m_line_series->setUseOpenGL(true);
    m_spline_series = new QSplineSeries();
    m_spline_series->setUseOpenGL(true);
}
void QWaveChartView::mouseMoveEvent(QMouseEvent *event)
{
    QMouseEvent *MouseEvent = static_cast<QMouseEvent *>(event);
    if ((event->type() == QEvent::MouseMove) &&
        (MouseEvent->buttons() == Qt::LeftButton))
    {
        QValueAxis *axisX = dynamic_cast<QValueAxis*>(m_chart->axisX());
        if(axisX->min()>0 && (m_pt-event->pos()).x()<0)
            m_chart->scroll((m_pt-event->pos()).x(),0);
        else if(axisX->max()<m_max-1 && (m_pt-event->pos()).x()>0)
            m_chart->scroll((m_pt-event->pos()).x(),0);
    }
    m_pt = event->pos();
}
void QWaveChartView::wheelEvent(QWheelEvent *event)
{
    if(m_chart->series().size()==0) return;
    if((event->modifiers()&Qt::ControlModifier) != 0 )
    {
        double xMin,xMax;
        QPointF curVal = m_chart->mapToValue(QPointF(event->pos()),m_chart->series().last());
        QValueAxis *axisX = dynamic_cast<QValueAxis*>(m_chart->axisX());
        if(event->delta() > 0){
            double offsetmax = 1/2.0f*(axisX->max()-curVal.x());
            double offsetmin = 1/2.0f*(curVal.x()-axisX->min());
            xMin = curVal.x()-offsetmin<0?0:curVal.x()-offsetmin;
            xMax = curVal.x()+offsetmax>m_max-1?m_max-1:curVal.x()+offsetmax;
    //        if(xMax-xMin<=500) {
    //            if(m_spline_series->count()>0)
    //                m_spline_series->clear();
    //            for(int i=xMin;i<xMax;i++)
    //                m_spline_series->append(m_line_series->at(i));

    //            if(m_chart->series().count()>0)
    //                m_chart->removeSeries(m_chart->series().last());
    //            m_chart->addSeries(m_spline_series);
    //            //m_series->setUseOpenGL(false);
    //        }
        }
        else{
            double offsetmax = 2.0f*(axisX->max()-curVal.x());
            double offsetmin = 2.0f*(curVal.x()-axisX->min());
            xMin = curVal.x()-offsetmin<0?0:curVal.x()-offsetmin;
            xMax = curVal.x()+offsetmax>m_max-1?m_max-1:curVal.x()+offsetmax;

    //        if(m_chart->series().count()>0)
    //            m_chart->removeSeries(m_chart->series().last());
    //        m_chart->addSeries(m_line_series);
            //m_series->setUseOpenGL(true);
        }
        axisX->setRange(xMin,xMax);
    }
}
void QWaveChartView::paintLine(QList<QList<QPointF>>& pts)
{
    m_line_series->clear();
    m_chart->removeAllSeries();
    for(int i=0;i<pts.size();i++)
    {
        if(m_max <pts[i].count()) m_max = pts[i].count();
        m_line_series->append(new QLineSeries());
        m_line_series->at(i)->replace(pts[i]);
        m_line_series->at(i)->setUseOpenGL(true);
        m_chart->addSeries(m_line_series->at(i));
    }
//    m_spline_series->setPointLabelsFormat("(@xPoint,@yPoint)");
//    m_spline_series->setPointLabelsVisible(true);
//    m_spline_series->setPointsVisible(true);
//    if(m_chart->series().count()>0)
//        m_chart->removeSeries(m_chart->series().last());

    m_chart->createDefaultAxes();
}
